import {
  getLanguage,
  getPersonnel,
  getBrand,
  getLogistics,
  getCurrency,
  getColumns,
  getWareData,
  getPlatform,
  getSupplie,
  getDepartment
} from '@/api/pmsIndex'
import cache from '@/plugins/cache'

const params = {
  pageNum: 1, pageSize: 999
}
const useCommonStore = defineStore(
  'common',
  {
    state: () => ({
      options: {},
      languageOptions: cache.session.getJSON('languages') || '', // 语言列表 22
      personnelOptions: cache.session.getJSON('personnel') || '', // 人员
      departmentOptions: cache.session.getJSON('department') || '', // 部门
      brandOption: '',  // 品牌
      logisticsOptions: '',  // 物流
      currencyOptions:'',  // 币种
      categoryOptions: '',  // 分类 （过滤禁用后）
      warehouseOptions: '', // 仓库
      platformOptions: cache.session.getJSON('platform') || '', //平台  22
      supplieOptions: '', //供应商
      shopOptions: [
        {
          label: '店铺1',
          value: 1
        },
        {
          label: '店铺2',
          value: 2
        },
        {
          label: '店铺3',
          value: 3
        },
        {
          label: '店铺4',
          value: 4
        },
      ],
    }),
    actions: {
      setOptions(key, value) {
        this.options[key] = value
      },
      getLanguageList() {
        return new Promise((resolve, reject) => {
          getLanguage(params).then(res => {
            cache.session.setJSON('languages', res.data)
            this.languageOptions = res.data
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      getPersonnel() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getPersonnel()
            cache.session.setJSON('personnel', res.data)
            this.personnelOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getDepartment() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getDepartment()
            cache.session.setJSON('department', res.data)
            this.departmentOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getBrandList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getBrand()
            // cache.session.setJSON('brand', res.data)
            this.brandOption = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getLogisticsList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getLogistics({
              pageSize: 1000
            })
            // cache.session.setJSON('logistics', res.data)
            this.logisticsOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getCurrencyList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCurrency()
            // cache.session.setJSON('currency', res.data)
            this.currencyOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getColumnsList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getColumns()
            // cache.session.setJSON('category', res.data)
            this.categoryOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getWarehouseList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData()
            // cache.session.setJSON('warehouse', res.data)
            this.warehouseOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getPlatformList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getPlatform()
            cache.session.setJSON('platform', res.data)
            this.platformOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getSupplieList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getSupplie()
            // cache.session.setJSON('supplie', res.data)
            this.supplieOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
    }
  })

export default useCommonStore
